home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / kcl / akcl / akcl1615.lha / c / unexelf.c < prev    next >
Text File  |  1991-05-21  |  23KB  |  628 lines

  1. /* Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc.
  2.  
  3.     This program is free software; you can redistribute it and/or modify
  4.     it under the terms of the GNU General Public License as published by
  5.     the Free Software Foundation; either version 1, or (at your option)
  6.     any later version.
  7.  
  8.     This program is distributed in the hope that it will be useful,
  9.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.     GNU General Public License for more details.
  12.  
  13.     You should have received a copy of the GNU General Public License
  14.     along with this program; if not, write to the Free Software
  15.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  
  17. In other words, you are welcome to use, share and improve this program.
  18. You are forbidden to forbid anyone else to use, share and improve
  19. what you give them.   Help stamp out software-hoarding!  */
  20.  
  21.  
  22. /*
  23.  * unexec.c - Convert a running program into an a.out file.
  24.  *
  25.  * Author:    Spencer W. Thomas
  26.  *         Computer Science Dept.
  27.  *         University of Utah
  28.  * Date:    Tue Mar  2 1982
  29.  * Modified heavily since then.
  30.  *
  31.  * Synopsis:
  32.  *    unexec (new_name, a_name, data_start, bss_start, entry_address)
  33.  *    char *new_name, *a_name;
  34.  *    unsigned data_start, bss_start, entry_address;
  35.  *
  36.  * Takes a snapshot of the program and makes an a.out format file in the
  37.  * file named by the string argument new_name.
  38.  * If a_name is non-NULL, the symbol table will be taken from the given file.
  39.  * On some machines, an existing a_name file is required.
  40.  *
  41.  * The boundaries within the a.out file may be adjusted with the data_start
  42.  * and bss_start arguments.  Either or both may be given as 0 for defaults.
  43.  *
  44.  * Data_start gives the boundary between the text segment and the data
  45.  * segment of the program.  The text segment can contain shared, read-only
  46.  * program code and literal data, while the data segment is always unshared
  47.  * and unprotected.  Data_start gives the lowest unprotected address.
  48.  * The value you specify may be rounded down to a suitable boundary
  49.  * as required by the machine you are using.
  50.  *
  51.  * Specifying zero for data_start means the boundary between text and data
  52.  * should not be the same as when the program was loaded.
  53.  * If NO_REMAP is defined, the argument data_start is ignored and the
  54.  * segment boundaries are never changed.
  55.  *
  56.  * Bss_start indicates how much of the data segment is to be saved in the
  57.  * a.out file and restored when the program is executed.  It gives the lowest
  58.  * unsaved address, and is rounded up to a page boundary.  The default when 0
  59.  * is given assumes that the entire data segment is to be stored, including
  60.  * the previous data and bss as well as any additional storage allocated with
  61.  * break (2).
  62.  *
  63.  * The new file is set up to start at entry_address.
  64.  *
  65.  * If you make improvements I'd like to get them too.
  66.  * harpo!utah-cs!thomas, thomas@Utah-20
  67.  *
  68.  */
  69.  
  70. /* Even more heavily modified by james@bigtex.cactus.org of Dell Computer Co.
  71.  * ELF support added.
  72.  *
  73.  * Basic theory: the data space of the running process needs to be
  74.  * dumped to the output file.  Normally we would just enlarge the size
  75.  * of .data, scooting everything down.  But we can't do that in ELF,
  76.  * because there is often something between the .data space and the
  77.  * .bss space.
  78.  *
  79.  * In the temacs dump below, notice that the Global Offset Table
  80.  * (.got) and the Dynamic link data (.dynamic) come between .data1 and
  81.  * .bss.  It does not work to overlap .data with these fields.
  82.  *
  83.  * The solution is to create a new .data segment.  This segment is
  84.  * filled with data from the current process.  Since the contents of
  85.  * various sections refer to sections by index, the new .data segment
  86.  * is made the last in the table to avoid changing any existing index.
  87.  
  88.  * This is an example of how the section headers are changed.  "Addr"
  89.  * is a process virtual address.  "Offset" is a file offset.
  90.  
  91. raid:/nfs/raid/src/dist-18.56/src> dump -h temacs
  92.  
  93. temacs:
  94.  
  95.            **** SECTION HEADER TABLE ****
  96. [No]    Type    Flags   Addr         Offset       Size          Name
  97.         Link    Info    Adralgn      Entsize
  98.  
  99. [1]     1       2       0x80480d4    0xd4         0x13          .interp
  100.         0       0       0x1          0            
  101.  
  102. [2]     5       2       0x80480e8    0xe8         0x388         .hash
  103.         3       0       0x4          0x4          
  104.  
  105. [3]     11      2       0x8048470    0x470        0x7f0         .dynsym
  106.         4       1       0x4          0x10         
  107.  
  108. [4]     3       2       0x8048c60    0xc60        0x3ad         .dynstr
  109.         0       0       0x1          0            
  110.  
  111. [5]     9       2       0x8049010    0x1010       0x338         .rel.plt
  112.         3       7       0x4          0x8          
  113.  
  114. [6]     1       6       0x8049348    0x1348       0x3           .init
  115.         0       0       0x4          0            
  116.  
  117. [7]     1       6       0x804934c    0x134c       0x680         .plt
  118.         0       0       0x4          0x4          
  119.  
  120. [8]     1       6       0x80499cc    0x19cc       0x3c56f       .text
  121.         0       0       0x4          0            
  122.  
  123. [9]     1       6       0x8085f3c    0x3df3c      0x3           .fini
  124.         0       0       0x4          0            
  125.  
  126. [10]    1       2       0x8085f40    0x3df40      0x69c         .rodata
  127.         0       0       0x4          0            
  128.  
  129. [11]    1       2       0x80865dc    0x3e5dc      0xd51         .rodata1
  130.         0       0       0x4          0            
  131.  
  132. [12]    1       3       0x8088330    0x3f330      0x20afc       .data
  133.         0       0       0x4          0            
  134.  
  135. [13]    1       3       0x80a8e2c    0x5fe2c      0x89d         .data1
  136.         0       0       0x4          0            
  137.  
  138. [14]    1       3       0x80a96cc    0x606cc      0x1a8         .got
  139.         0       0       0x4          0x4          
  140.  
  141. [15]    6       3       0x80a9874    0x60874      0x80          .dynamic
  142.         4       0       0x4          0x8          
  143.  
  144. [16]    8       3       0x80a98f4    0x608f4      0x449c        .bss
  145.         0       0       0x4          0            
  146.  
  147. [17]    2       0       0            0x608f4      0x9b90        .symtab
  148.         18      371     0x4          0x10         
  149.  
  150. [18]    3       0       0            0x6a484      0x8526        .strtab
  151.         0       0       0x1          0            
  152.  
  153. [19]    3       0       0            0x729aa      0x93          .shstrtab
  154.         0       0       0x1          0            
  155.  
  156. [20]    1       0       0            0x72a3d      0x68b7        .comment
  157.         0       0       0x1          0            
  158.  
  159. raid:/nfs/raid/src/dist-18.56/src> dump -h xemacs
  160.  
  161. xemacs:
  162.  
  163.            **** SECTION HEADER TABLE ****
  164. [No]    Type    Flags   Addr         Offset       Size          Name
  165.         Link    Info    Adralgn      Entsize
  166.  
  167. [1]     1       2       0x80480d4    0xd4         0x13          .interp
  168.         0       0       0x1          0            
  169.  
  170. [2]     5       2       0x80480e8    0xe8         0x388         .hash
  171.         3       0       0x4          0x4          
  172.  
  173. [3]     11      2       0x8048470    0x470        0x7f0         .dynsym
  174.         4       1       0x4          0x10         
  175.  
  176. [4]     3       2       0x8048c60    0xc60        0x3ad         .dynstr
  177.         0       0       0x1          0            
  178.  
  179. [5]     9       2       0x8049010    0x1010       0x338         .rel.plt
  180.         3       7       0x4          0x8          
  181.  
  182. [6]     1       6       0x8049348    0x1348       0x3           .init
  183.         0       0       0x4          0            
  184.  
  185. [7]     1       6       0x804934c    0x134c       0x680         .plt
  186.         0       0       0x4          0x4          
  187.  
  188. [8]     1       6       0x80499cc    0x19cc       0x3c56f       .text
  189.         0       0       0x4          0            
  190.  
  191. [9]     1       6       0x8085f3c    0x3df3c      0x3           .fini
  192.         0       0       0x4          0            
  193.  
  194. [10]    1       2       0x8085f40    0x3df40      0x69c         .rodata
  195.         0       0       0x4          0            
  196.  
  197. [11]    1       2       0x80865dc    0x3e5dc      0xd51         .rodata1
  198.         0       0       0x4          0            
  199.  
  200. [12]    1       3       0x8088330    0x3f330      0x20afc       .data
  201.         0       0       0x4          0            
  202.  
  203. [13]    1       3       0x80a8e2c    0x5fe2c      0x89d         .data1
  204.         0       0       0x4          0            
  205.  
  206. [14]    1       3       0x80a96cc    0x606cc      0x1a8         .got
  207.         0       0       0x4          0x4          
  208.  
  209. [15]    6       3       0x80a9874    0x60874      0x80          .dynamic
  210.         4       0       0x4          0x8          
  211.  
  212. [16]    8       3       0x80c6800    0x7d800      0             .bss
  213.         0       0       0x4          0            
  214.  
  215. [17]    2       0       0            0x7d800      0x9b90        .symtab
  216.         18      371     0x4          0x10         
  217.  
  218. [18]    3       0       0            0x87390      0x8526        .strtab
  219.         0       0       0x1          0            
  220.  
  221. [19]    3       0       0            0x8f8b6      0x93          .shstrtab
  222.         0       0       0x1          0            
  223.  
  224. [20]    1       0       0            0x8f949      0x68b7        .comment
  225.         0       0       0x1          0            
  226.  
  227. [21]    1       3       0x80a98f4    0x608f4      0x1cf0c       .data
  228.         0       0       0x4          0            
  229.  
  230.  * This is an example of how the file header is changed.  "Shoff" is
  231.  * the section header offset within the file.  Since that table is
  232.  * after the new .data section, it is moved.  "Shnum" is the number of
  233.  * sections, which we increment.
  234.  *
  235.  * "Phoff" is the file offset to the program header.  "Phentsize" and
  236.  * "Shentsz" are the program and section header entries sizes respectively.
  237.  * These can be larger than the apparent struct sizes.
  238.  
  239. raid:/nfs/raid/src/dist-18.56/src> dump -f temacs
  240.  
  241. temacs:
  242.  
  243.                     **** ELF HEADER ****
  244. Class        Data       Type         Machine     Version
  245. Entry        Phoff      Shoff        Flags       Ehsize
  246. Phentsize    Phnum      Shentsz      Shnum       Shstrndx
  247.  
  248. 1            1          2            3           1
  249. 0x80499cc    0x34       0x792f4      0           0x34
  250. 0x20         5          0x28         21          19
  251.  
  252. raid:/nfs/raid/src/dist-18.56/src> dump -f xemacs
  253.  
  254. xemacs:
  255.  
  256.                     **** ELF HEADER ****
  257. Class        Data       Type         Machine     Version
  258. Entry        Phoff      Shoff        Flags       Ehsize
  259. Phentsize    Phnum      Shentsz      Shnum       Shstrndx
  260.  
  261. 1            1          2            3           1
  262. 0x80499cc    0x34       0x96200      0           0x34
  263. 0x20         5          0x28         22          19
  264.  
  265.  * These are the program headers.  "Offset" is the file offset to the
  266.  * segment.  "Vaddr" is the memory load address.  "Filesz" is the
  267.  * segment size as it appears in the file, and "Memsz" is the size in
  268.  * memory.  Below, the third segment is the code and the fourth is the
  269.  * data: the difference between Filesz and Memsz is .bss
  270.  
  271. raid:/nfs/raid/src/dist-18.56/src> dump -o temacs
  272.  
  273. temacs:
  274.  ***** PROGRAM EXECUTION HEADER *****
  275. Type        Offset      Vaddr       Paddr
  276. Filesz      Memsz       Flags       Align
  277.  
  278. 6           0x34        0x8048034   0           
  279. 0xa0        0xa0        5           0           
  280.  
  281. 3           0xd4        0           0           
  282. 0x13        0           4           0           
  283.  
  284. 1           0x34        0x8048034   0           
  285. 0x3f2f9     0x3f2f9     5           0x1000      
  286.  
  287. 1           0x3f330     0x8088330   0           
  288. 0x215c4     0x25a60     7           0x1000      
  289.  
  290. 2           0x60874     0x80a9874   0           
  291. 0x80        0           7           0           
  292.  
  293. raid:/nfs/raid/src/dist-18.56/src> dump -o xemacs
  294.  
  295. xemacs:
  296.  ***** PROGRAM EXECUTION HEADER *****
  297. Type        Offset      Vaddr       Paddr
  298. Filesz      Memsz       Flags       Align
  299.  
  300. 6           0x34        0x8048034   0           
  301. 0xa0        0xa0        5           0           
  302.  
  303. 3           0xd4        0           0           
  304. 0x13        0           4           0           
  305.  
  306. 1           0x34        0x8048034   0           
  307. 0x3f2f9     0x3f2f9     5           0x1000      
  308.  
  309. 1           0x3f330     0x8088330   0           
  310. 0x3e4d0     0x3e4d0     7           0x1000      
  311.  
  312. 2           0x60874     0x80a9874   0           
  313. 0x80        0           7           0           
  314.  
  315.  
  316.  */
  317.  
  318. #include <sys/types.h>
  319. #include <stdio.h>
  320. #include <sys/stat.h>
  321. #include <memory.h>
  322. #include <string.h>
  323. #include <errno.h>
  324. #include <unistd.h>
  325. #include <fcntl.h>
  326. #include <elf.h>
  327. #include <sys/mman.h>
  328.  
  329. #ifndef emacs
  330. #define fatal(a, b, c) fprintf(stderr, a, b, c), exit(1)
  331. #else
  332. extern void fatal(char *, ...);
  333. #endif
  334.  
  335. /* Get the address of a particular section or program header entry,
  336.  * accounting for the size of the entries.
  337.  */
  338.  
  339. #define OLD_SECTION_H(n) \
  340.      (*(Elf32_Shdr *) ((byte *) old_section_h + old_file_h->e_shentsize * (n)))
  341. #define NEW_SECTION_H(n) \
  342.      (*(Elf32_Shdr *) ((byte *) new_section_h + new_file_h->e_shentsize * (n)))
  343. #define OLD_PROGRAM_H(n) \
  344.      (*(Elf32_Phdr *) ((byte *) old_program_h + old_file_h->e_phentsize * (n)))
  345. #define NEW_PROGRAM_H(n) \
  346.      (*(Elf32_Phdr *) ((byte *) new_program_h + new_file_h->e_phentsize * (n)))
  347.  
  348. typedef unsigned char byte;
  349.  
  350. /* ****************************************************************
  351.  * unexec
  352.  *
  353.  * driving logic.
  354.  *
  355.  * In ELF, this works by replacing the old .bss section with a new
  356.  * .data section, and inserting an empty .bss immediately afterwards.
  357.  *
  358.  */
  359. void
  360. unexec (new_name, old_name, data_start, bss_start, entry_address)
  361.      char *new_name, *old_name;
  362.      unsigned data_start, bss_start, entry_address;
  363. {
  364. /*  extern unsigned int bss_end; */
  365. #define bss_end core_end
  366. #define emacs  
  367.   int new_file, old_file, new_file_size;
  368.  
  369.   /* Pointers to the base of the image of the two files. */
  370.   caddr_t old_base, new_base;
  371.  
  372.   /* Pointers to the file, program and section headers for the old and new
  373.    * files.
  374.    */
  375.   Elf32_Ehdr *old_file_h, *new_file_h;
  376.   Elf32_Phdr *old_program_h, *new_program_h;
  377.   Elf32_Shdr *old_section_h, *new_section_h;
  378.  
  379.   /* Point to the section name table in the old file */
  380.   char *old_section_names;
  381.  
  382.   Elf32_Addr old_bss_addr, new_bss_addr;
  383.   Elf32_Word old_bss_size, new_data2_size;
  384.   Elf32_Off  new_data2_offset;
  385.   Elf32_Addr new_data2_addr;
  386.  
  387.   int n, old_bss_index, old_data_index, new_data2_index;
  388.   struct stat stat_buf;
  389.  
  390.   /* Open the old file & map it into the address space. */
  391.  
  392.   old_file = open (old_name, O_RDONLY);
  393.  
  394.   if (old_file < 0)
  395.     fatal ("Can't open %s for reading: errno %d\n", old_name, errno);
  396.  
  397.   if (fstat (old_file, &stat_buf) == -1)
  398.     fatal ("Can't fstat(%s): errno %d\n", old_name, errno);
  399.  
  400.   old_base = mmap (0, stat_buf.st_size, PROT_READ, MAP_SHARED, old_file, 0);
  401.  
  402.   if (old_base == (caddr_t) -1)
  403.     fatal ("Can't mmap(%s): errno %d\n", old_name, errno);
  404.  
  405. #ifdef DEBUG
  406.   fprintf (stderr, "mmap(%s, %x) -> %x\n", old_name, stat_buf.st_size,
  407.        old_base);
  408. #endif
  409.  
  410.   /* Get pointers to headers & section names */
  411.  
  412.   old_file_h = (Elf32_Ehdr *) old_base;
  413.   old_program_h = (Elf32_Phdr *) ((byte *) old_base + old_file_h->e_phoff);
  414.   old_section_h = (Elf32_Shdr *) ((byte *) old_base + old_file_h->e_shoff);
  415.   old_section_names = (char *) old_base
  416.     + OLD_SECTION_H(old_file_h->e_shstrndx).sh_offset;
  417.  
  418.   /* Find the old .bss section.  Figure out parameters of the new
  419.    * data2 and bss sections.
  420.    */
  421.  
  422.   for (old_bss_index = 1; old_bss_index < old_file_h->e_shnum; old_bss_index++)
  423.     {
  424. #ifdef DEBUG
  425.       fprintf (stderr, "Looking for .bss - found %s\n",
  426.            old_section_names + OLD_SECTION_H(old_bss_index).sh_name);
  427. #endif
  428.       if (!strcmp (old_section_names + OLD_SECTION_H(old_bss_index).sh_name,
  429.            ".bss"))
  430.     break;
  431.     }
  432.   if (old_bss_index == old_file_h->e_shnum)
  433.     fatal ("Can't find .bss in %s.\n", old_name, 0);
  434.  
  435.   old_bss_addr = OLD_SECTION_H(old_bss_index).sh_addr;
  436.   old_bss_size = OLD_SECTION_H(old_bss_index).sh_size;
  437. #if defined(emacs) || !defined(DEBUG)
  438.   bss_end = (unsigned int) sbrk (0);
  439.   new_bss_addr = (Elf32_Addr) bss_end;
  440. #else
  441.   new_bss_addr = old_bss_addr + old_bss_size + 0x1234;
  442. #endif
  443.   new_data2_addr = old_bss_addr;
  444.   new_data2_size = new_bss_addr - old_bss_addr;
  445.   new_data2_offset = OLD_SECTION_H(old_bss_index).sh_offset;
  446.  
  447. #ifdef DEBUG
  448.   fprintf (stderr, "old_bss_index %d\n", old_bss_index);
  449.   fprintf (stderr, "old_bss_addr %x\n", old_bss_addr);
  450.   fprintf (stderr, "old_bss_size %x\n", old_bss_size);
  451.   fprintf (stderr, "new_bss_addr %x\n", new_bss_addr);
  452.   fprintf (stderr, "new_data2_addr %x\n", new_data2_addr);
  453.   fprintf (stderr, "new_data2_size %x\n", new_data2_size);
  454.   fprintf (stderr, "new_data2_offset %x\n", new_data2_offset);
  455. #endif
  456.  
  457.   if ((unsigned) new_bss_addr < (unsigned) old_bss_addr + old_bss_size)
  458.     fatal (".bss shrank when undumping???\n", 0, 0);
  459.  
  460.   /* Set the output file to the right size and mmap(2) it.  Set
  461.    * pointers to various interesting objects.  stat_buf still has
  462.    * old_file data.
  463.    */
  464.  
  465.   new_file = open (new_name, O_RDWR | O_CREAT, 0666);
  466.   if (new_file < 0)
  467.     fatal ("Can't creat(%s): errno %d\n", new_name, errno);
  468.  
  469.   new_file_size = stat_buf.st_size + old_file_h->e_shentsize + new_data2_size;
  470.  
  471.   if (ftruncate (new_file, new_file_size))
  472.     fatal ("Can't ftruncate(%s): errno %d\n", new_name, errno);
  473.  
  474.   new_base = mmap (0, new_file_size, PROT_READ | PROT_WRITE, MAP_SHARED,
  475.            new_file, 0);
  476.  
  477.   if (new_base == (caddr_t) -1)
  478.     fatal ("Can't mmap(%s): errno %d\n", new_name, errno);
  479.  
  480.   new_file_h = (Elf32_Ehdr *) new_base;
  481.   new_program_h = (Elf32_Phdr *) ((byte *) new_base + old_file_h->e_phoff);
  482.   new_section_h = (Elf32_Shdr *)
  483.     ((byte *) new_base + old_file_h->e_shoff + new_data2_size);
  484.  
  485.   /* Make our new file, program and section headers as copies of the
  486.    * originals.
  487.    */
  488.  
  489.   memcpy (new_file_h, old_file_h, old_file_h->e_ehsize);
  490.   memcpy (new_program_h, old_program_h,
  491.       old_file_h->e_phnum * old_file_h->e_phentsize);
  492.   memcpy (new_section_h, old_section_h,
  493.       old_file_h->e_shnum * old_file_h->e_shentsize);
  494.  
  495.   /* Fix up file header.  We'll add one section.  Section header is
  496.    * further away now.
  497.    */
  498.  
  499.   new_file_h->e_shoff += new_data2_size;
  500.   new_file_h->e_shnum += 1;
  501.  
  502. #ifdef DEBUG
  503.   fprintf (stderr, "Old section offset %x\n", old_file_h->e_shoff);
  504.   fprintf (stderr, "Old section count %d\n", old_file_h->e_shnum);
  505.   fprintf (stderr, "New section offset %x\n", new_file_h->e_shoff);
  506.   fprintf (stderr, "New section count %d\n", new_file_h->e_shnum);
  507. #endif
  508.  
  509.   /* Fix up a new program header.  Extend the writable data segment so
  510.    * that the bss area is covered too. Find that segment by looking
  511.    * for a segment that ends just before the .bss area.  Make sure
  512.    * that no segments are above the new .data2.  Put a loop at the end
  513.    * to adjust the offset and address of any segment that is above
  514.    * data2, just in case we decide to allow this later.
  515.    */
  516.  
  517.   for (n = new_file_h->e_phnum - 1; n >= 0; n--)
  518.     {
  519.       if (NEW_PROGRAM_H(n).p_vaddr + NEW_PROGRAM_H(n).p_filesz > old_bss_addr)
  520.     fatal ("Program segment above .bss in %s\n", old_name, 0);
  521.  
  522.       if (NEW_PROGRAM_H(n).p_type == PT_LOAD
  523.       && (NEW_PROGRAM_H(n).p_vaddr + NEW_PROGRAM_H(n).p_filesz
  524.           == old_bss_addr))
  525.     break;
  526.     }
  527.   if (n < 0)
  528.     fatal ("Couldn't find segment next to .bss in %s\n", old_name, 0);
  529.  
  530.   NEW_PROGRAM_H(n).p_filesz += new_data2_size;
  531.   NEW_PROGRAM_H(n).p_memsz = NEW_PROGRAM_H(n).p_filesz;
  532.  
  533. #if 0 /* Maybe allow section after data2 - does this ever happen? */
  534.   for (n = new_file_h->e_phnum - 1; n >= 0; n--)
  535.     {
  536.       if (NEW_PROGRAM_H(n).p_vaddr
  537.       && NEW_PROGRAM_H(n).p_vaddr >= new_data2_addr)
  538.     NEW_PROGRAM_H(n).p_vaddr += new_data2_size - old_bss_size;
  539.  
  540.       if (NEW_PROGRAM_H(n).p_offset >= new_data2_offset)
  541.     NEW_PROGRAM_H(n).p_offset += new_data2_size;
  542.     }
  543. #endif
  544.  
  545.   /* Fix up section headers based on new .data2 section.  Any section
  546.    * whose offset or virtual address is after the new .data2 section
  547.    * gets its value adjusted.  .bss size becomes zero and new address
  548.    * is set.  data2 section header gets added by copying the existing
  549.    * .data header and modifying the offset, address and size.
  550.    */
  551.  
  552.   for (n = 1; n < new_file_h->e_shnum; n++)
  553.     {
  554.       if (NEW_SECTION_H(n).sh_offset >= new_data2_offset)
  555.     NEW_SECTION_H(n).sh_offset += new_data2_size;
  556.  
  557.       if (NEW_SECTION_H(n).sh_addr
  558.       && NEW_SECTION_H(n).sh_addr >= new_data2_addr)
  559.     NEW_SECTION_H(n).sh_addr += new_data2_size - old_bss_size;
  560.     }
  561.  
  562.   new_data2_index = old_file_h->e_shnum;
  563.  
  564.   for (old_data_index = 1; old_data_index < old_file_h->e_shnum;
  565.        old_data_index++)
  566.     if (!strcmp (old_section_names + OLD_SECTION_H(old_data_index).sh_name,
  567.          ".data"))
  568.       break;
  569.   if (old_data_index == old_file_h->e_shnum)
  570.     fatal ("Can't find .data in %s.\n", old_name, 0);
  571.  
  572.   memcpy (&NEW_SECTION_H(new_data2_index), &OLD_SECTION_H(old_data_index),
  573.       new_file_h->e_shentsize);
  574.  
  575.   NEW_SECTION_H(new_data2_index).sh_addr = new_data2_addr;
  576.   NEW_SECTION_H(new_data2_index).sh_offset = new_data2_offset;
  577.   NEW_SECTION_H(new_data2_index).sh_size = new_data2_size;
  578.  
  579.   NEW_SECTION_H(old_bss_index).sh_size = 0;
  580.   NEW_SECTION_H(old_bss_index).sh_addr = new_data2_addr + new_data2_size;
  581.  
  582.   /* Write out the sections. .data and .data1 (and data2, called
  583.    * ".data" in the strings table) get copied from the current process
  584.    * instead of the old file.
  585.    */
  586.  
  587.   for (n = new_file_h->e_shnum - 1; n; n--)
  588.     {
  589.       caddr_t src;
  590.  
  591.       if (NEW_SECTION_H(n).sh_type == SHT_NULL
  592.       || NEW_SECTION_H(n).sh_type == SHT_NOBITS)
  593.     continue;
  594.  
  595.       if (!strcmp (old_section_names + NEW_SECTION_H(n).sh_name, ".data")
  596.       || !strcmp ((old_section_names + NEW_SECTION_H(n).sh_name),
  597.               ".data1"))
  598.     src = (caddr_t) NEW_SECTION_H(n).sh_addr;
  599.       else
  600.     src = old_base + OLD_SECTION_H(n).sh_offset;
  601.  
  602.       memcpy (NEW_SECTION_H(n).sh_offset + new_base, src,
  603.           NEW_SECTION_H(n).sh_size);
  604.     }
  605.  
  606.   /* Close the files and make the new file executable */
  607.  
  608.   if (close (old_file))
  609.     fatal ("Can't close(%s): errno %d\n", old_name, errno);
  610.  
  611.   if (close (new_file))
  612.     fatal ("Can't close(%s): errno %d\n", new_name, errno);
  613.  
  614.   if (stat (new_name, &stat_buf) == -1)
  615.     fatal ("Can't stat(%s): errno %d\n", new_name, errno);
  616.  
  617.   n = umask (777);
  618.   umask (n);
  619.   stat_buf.st_mode |= 0111 & ~n;
  620.   if (chmod (new_name, stat_buf.st_mode) == -1)
  621.     fatal ("Can't chmod(%s): errno %d\n", new_name, errno);
  622. }
  623.  
  624.  
  625. #ifdef UNIXSAVE
  626. #include "save.c"
  627. #endif
  628.